package dao;

import model.Goods;
import model.Order;
import util.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDao {
    JDBCUtil jdbcUtil = new JDBCUtil();

    // 插入新订单
    public boolean insertOrder(Order order) {
        String sql = "INSERT INTO orders (user_id, recipient_name, recipient_address, recipient_phone, notes, order_date, order_status, order_total) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        return jdbcUtil.paramsUpdate(sql, order.getUser_id(), order.getRecipient_name(), order.getRecipient_address(), order.getRecipient_phone(), order.getNotes(), order.getOrder_date(), order.getOrder_status(), order.getOrder_total());
    }

    // 查询所有订单
    public List<Order> getAllOrders() {
        List<Order> orderList = new ArrayList<>();
        ResultSet resultSet = null;

        try {
            String sql = "SELECT * FROM orders";
            resultSet = jdbcUtil.prepareQuery(sql);
            while (resultSet.next()) {
                Order order = new Order();
                order.setOrder_id(resultSet.getInt("order_id"));
                order.setUser_id(resultSet.getInt("user_id"));
                order.setRecipient_name(resultSet.getString("recipient_name"));
                order.setRecipient_address(resultSet.getString("recipient_address"));
                order.setRecipient_phone(resultSet.getString("recipient_phone"));
                order.setNotes(resultSet.getString("notes"));
                order.setOrder_date(resultSet.getString("order_date"));
                order.setOrder_status(resultSet.getString("order_status"));
                order.setOrder_total(resultSet.getDouble("order_total"));
                orderList.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            jdbcUtil.closeConnection();
        }

        return orderList;
    }

    // 根据ID删除订单
    public boolean deleteOrderById(int orderId) {
        try {
            String sql = "DELETE FROM orders WHERE order_id = ?";
            return jdbcUtil.paramsUpdate(sql, orderId);
        } finally {
            jdbcUtil.closeConnection();
        }
    }

    // 更新订单信息
//    public boolean updateOrder(Order order) {
//        String sql = "UPDATE orders SET user_id = ?, recipient_name = ?, recipient_address = ?, recipient_phone = ?, notes = ?, order_date = ?, order_status = ?, order_total = ? WHERE order_id = ?";
//        return jdbcUtil.paramsUpdate(sql, order.getUser_id(), order.getRecipient_name(), order.getRecipient_address(), order.getRecipient_phone(), order.getNotes(), order.getOrder_date(), order.getOrder_status(), order.getOrder_total(), order.getOrder_id());
//    }

    // 根据用户ID查找订单
    public List<Order> getOrdersByUserId(int userId) {
        List<Order> orderList = new ArrayList<>();
        ResultSet resultSet = null;

        try {
            String sql = "SELECT * FROM orders WHERE user_id = ?";
            resultSet = jdbcUtil.prepareQuery(sql, userId);
            while (resultSet.next()) {
                Order order = new Order();
                order.setOrder_id(resultSet.getInt("order_id"));
                order.setUser_id(resultSet.getInt("user_id"));
                order.setRecipient_name(resultSet.getString("recipient_name"));
                order.setRecipient_address(resultSet.getString("recipient_address"));
                order.setRecipient_phone(resultSet.getString("recipient_phone"));
                order.setNotes(resultSet.getString("notes"));
                order.setOrder_date(resultSet.getString("order_date"));
                order.setOrder_status(resultSet.getString("order_status"));
                order.setOrder_total(resultSet.getDouble("order_total"));
                orderList.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            jdbcUtil.closeConnection();
        }

        return orderList;
    }
    public boolean updateOrder(Order order) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql = "UPDATE `order` SET recipient_name = ?, recipient_address = ?, recipient_phone = ?, notes = ?, order_status = ?, order_total = ? WHERE order_id = ?";
        boolean result =false;
        try {
            result = jdbcUtil.paramsUpdate(sql, order.getRecipient_name(), order.getRecipient_address(), order.getRecipient_phone(), order.getNotes(), order.getOrder_status(), order.getOrder_total(), order.getOrder_id());
        } finally {
            jdbcUtil.closeConnection();
        }
        return result;
    }

    public Order getOrderByOrderId(int orderId) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql = "SELECT * FROM `order` WHERE order_id = ?";
        ResultSet resultSet = null;
        Order order = null;

        try {
            resultSet = jdbcUtil.prepareQuery(sql, orderId);
            if (resultSet.next()) {
                order = new Order();
                order.setOrder_id(resultSet.getInt("order_id"));
                order.setUser_id(resultSet.getInt("user_id"));
                order.setRecipient_name(resultSet.getString("recipient_name"));
                order.setRecipient_address(resultSet.getString("recipient_address"));
                order.setRecipient_phone(resultSet.getString("recipient_phone"));
                order.setNotes(resultSet.getString("notes"));
                order.setOrder_date(resultSet.getString("order_date"));
                order.setOrder_status(resultSet.getString("order_status"));
                order.setOrder_total(resultSet.getDouble("order_total"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                jdbcUtil.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return order;
    }
    // 根据订单状态查找订单
    public List<Order> getOrdersByStatus(String status) {
        List<Order> orderList = new ArrayList<>();
        ResultSet resultSet = null;

        try {
            String sql = "SELECT * FROM orders WHERE order_status = ?";
            resultSet = jdbcUtil.prepareQuery(sql, status);
            while (resultSet.next()) {
                Order order = new Order();
                order.setOrder_id(resultSet.getInt("order_id"));
                order.setUser_id(resultSet.getInt("user_id"));
                order.setRecipient_name(resultSet.getString("recipient_name"));
                order.setRecipient_address(resultSet.getString("recipient_address"));
                order.setRecipient_phone(resultSet.getString("recipient_phone"));
                order.setNotes(resultSet.getString("notes"));
                order.setOrder_date(resultSet.getString("order_date"));
                order.setOrder_status(resultSet.getString("order_status"));
                order.setOrder_total(resultSet.getDouble("order_total"));
                orderList.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            jdbcUtil.closeConnection();
        }

        return orderList;
    }
}
